<?php

namespace App\Admin\Controllers;

use App\Admin\Filters\TimestampBetween;
use App\Models\EntrySurvey;
use App\Models\Project;
use App\Models\Sweep;
use App\Models\Team;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Tab;

class SweepController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '已解散', 'color' => 'danger'],
        'off' => ['value' => 1, 'text' => '正常', 'color' => 'default'],
    ];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('扫街列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('扫街详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑扫街')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增扫街')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Sweep());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user_id', '创建者ID');

                $filter->equal('projectInfo.id', '项目')->select(Project::pluck('name as text', 'id'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->use(new TimestampBetween('created_at', __('created_at')))->datetime();
            });
        });
//
        $grid->model()->orderBy('created_at', 'desc');
        $grid->column('id', 'ID')->sortable();
        $grid->column('projectInfo.name', '项目')->sortable();
        $grid->column('team_id', '队伍ID')->sortable();
        $grid->column('teamInfo.name', '队伍名称')->sortable();
        $grid->column('sign_coord', '标记坐标')->display(function ($v) {
            return implode(',', $v);
        });
        $grid->column('sign_desc', '标记描述');
        $grid->column('sign_pics', '标记图片')->lightbox(['width' => '50', 'height' => '50']);
        $grid->column('userInfo.nickname', '创建者')->sortable();
        $grid->column('aa', '内容')->modal('入户调研详情', function ($model) {
            $array = EntrySurvey::where('sweep_id', $model->id)->value('content');
            if (!empty($array)) {
                return self::showContent($array);
            }
            return '暂无';
        });
        $grid->column('created_at', __('created_at'));
        $grid->disableActions();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
//
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableBatchActions();
        $grid->expandFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Team::findOrFail($id));
        $show->field('id', '队伍ID');
        $show->field('name', '队伍名称');
        $show->field('projectInfo.name', '项目');
        $show->field('userInfo.nickname', '创建者');
        $show->field('qty', '成员数');
        $show->field('status', '状态');
        $show->field('created_at', __('created_at'));
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Team());


        $form->radio('status', '状态')->options(self::INVITE_STATUS);
        $form->image('email', '图片');
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

    public static function showContent($array)
    {
        $tab = new Tab();
        foreach ($array as $item) {
            $form = new \Encore\Admin\Widgets\Form();
            foreach ($item['val_assign'] as $value) {
                switch ($value['input_type']) {
                    case 0:
                        $form->text($value['title'], $value['title'])->default($value['content'])->readonly();
                        break;
                    case 1:
                        $form->date($value['title'], $value['title'])->default($value['content'])->readonly();
                        break;
                    case 2:
                        $form->display($value['title'], $value['title'])->customFormat(function () use ($value) {
                            if (!empty($value['content'])) {
                                $html = '<div>';
                                foreach ($value['content'] as $val) {
                                    $html .= '<img style="margin:10px" width="200" height="200" src="'.$val.'" />';
                                }
                                $html.='</div>';
                                return $html;
                            }
                            return '';

                        });
                        break;
                    default:
//                        $form->file($value['title'], $value['title'])->fill($value['content']);
                        $form->display($value['title'], $value['title'])->customFormat(function () use ($value) {
                            if (!empty($value['content'])) {
                                $html = '<div>';
                                foreach ($value['content'] as $val) {
                                    $html .= '<video controls style="margin:10px" width="520" height="300" src="'.$val.'" />';
                                }
                                $html.='</div>';
                                return $html;
                            }
                            return '';

                        });
                        break;
                }

            }
            $tab->add($item['key_assign'], $form);
            $form->disableReset();
            $form->disableSubmit();
            unset($form);
        }
        return $tab;
    }


}
